// 1.导入Vue和VueRouter的包
import Vue from 'vue'
import VueRouter from 'vue-router'

//导入需要的组件
import Login from '@/views/Login/Login.vue'
import Home from '@/views/Home/Home.vue'
import Welcome from '@/views/Welcome/Welcome.vue'
import Users from '@/views/user/Users.vue'
import Rights from '@/views/power/Rights.vue'
import Roles from '@/views/power/Roles.vue'
import Cate from '@/views/goods/Cate.vue'
import Params from '@/views/goods/Params.vue'
import Goods from '@/views/goods/Good_List.vue'
import Add from '@/views/goods/Add.vue'
import Orders from '@/views/order/Orders.vue'
import Reports from '@/views/report/Reports.vue'
// 2.调用Vue.use()函数，把VueRouter安装为Vue的插件
Vue.use(VueRouter)
//4.制定路由规则
const routes = [
  //路由规则 
  //登录
  {path:'/login',component:Login},
  //路由重定向
  {path:'/',redirect:'/login'},
  {path:'/home',component:Home,redirect:'/home/welcome',children:[
    {path:'welcome',component:Welcome},
    {path:'/users',component:Users},
    {path:'/rights',component:Rights},
    {path:'/roles',component:Roles},
    {path:'/categories',component:Cate},
    {path:'/params',component:Params},
    {path:'/goods',component:Goods},
    {path:'/goods/add',component:Add},
    {path:'/orders',component:Orders},
    {path:'/reports',component:Reports},
  ]}
  
]
// 3.创建路由的实例对象
const router = new VueRouter({
  routes
})

//to 是将要访问的路由的信息对象;
  //from 是将要离开的路由的信息对象
  //next 是一个函数，调用 next() 表示放行，允许这次路由导航
//路由守卫 
router.beforeEach((to,from,next)=>{
  // 查找pathArr数组的内容的索引，只要索引不等于-1，就代表里面的地址匹配到了并拦截
  if(to.path === '/login') return next()
    const token = sessionStorage.getItem('token')
    if(!token) return next('/login')
    next()
})

// 5.向外共享路由的实例对象
export default router
